function autoSaveImg(url, callback, _textarea) {
    if (url.substring(0, 4).toLowerCase() === "http") {
        $.post("autosaveimg.php", {img: url, isurl: 1}, function (d) {
            console.log(d);
            callback.call(this, {textarea: _textarea, url: url, data: d});
        }).error(function () {
            callback.call(this, {textarea: _textarea, url: url, data: ""});
        })
    } else {
        var canvas = document.createElement('CANVAS'),
            ctx = canvas.getContext('2d'),
            img = new Image;
        img.crossOrigin = 'Anonymous';
        img.onload = function () {
            canvas.height = img.height;
            canvas.width = img.width;
            ctx.drawImage(img, 0, 0);
            var dataURL = canvas.toDataURL('image/png');
            $.post("autosaveimg.php", {img: dataURL}, function (d) {
                console.log(d);
                callback.call(this, {textarea: _textarea, url: url, data: d});
            }).error(function () {
                callback.call(this, {textarea: _textarea, url: url, data: ""});
            })
        }

        img.onerror = function () {
            callback.call(this, {textarea: _textarea, url: url, data: ""});
        }

        img.src = url;
    }
}

function convertImgToBase64(url, callback, _textarea) {
    var canvas = document.createElement('CANVAS'),
        ctx = canvas.getContext('2d'),
        img = new Image;
    img.crossOrigin = 'Anonymous';
    img.onload = function () {
        canvas.height = img.height;
        canvas.width = img.width;
        ctx.drawImage(img, 0, 0);
        var dataURL = canvas.toDataURL('image/png');
        callback.call(this, {textarea: _textarea, url: url, data: dataURL});
        canvas = null;
    };
    img.onerror = function () {
        callback.call(this, {textarea: _textarea, url: url, data: ""});
    }
    img.src = url;
}

var needReplace_arr = [];
var replace_count = 0;

function getImages(_this, last = 0) {
    var m_this_submit = $(_this).find("input[type=submit]");
    if (last) {
        m_this_submit.val("编辑");
        _this.submit();
        return;
    }
    var allTextArea = $("textarea").not(".ke-edit-textarea");

    function callback(m_object) {
        needReplace_arr.push(m_object);
        if (m_object && m_object.data) {
            var dd = $(m_object.textarea).val();
            dd = dd.replace(m_object.url, m_object.data);
            $(m_object.textarea).val(dd);
        }

        if (needReplace_arr.length == replace_count) {
            getImages(_this, 1);//提交
        }
    }

    allTextArea.each(function () {
        var m_textarea = this;
        var dd = $(this).val();


        m_this_submit.val("正在上传图片...");
        var regx = new RegExp(/<img[\s\S]*?src\s*=\s*[\"|\'](.*?)[\"|\'][\s\S]*?>/gi);
        var oked = dd.replace(regx, function (mm, c) {
            //convertImgToBase64(c, callback, m_textarea);
            if (c.substring(0, 4) == 'http' || c.substring(0, 4) == 'file') {
                autoSaveImg(c, callback, m_textarea);
                replace_count++;
            }
        });
    });
    if (replace_count > 0) {
        return false;
    } else {
        return true;
    }
}